package com.fdb.efp.esb.service.impl.yed;

import com.fdb.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.constant.EsbRetCodeStandard;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.basic.framework.core.util.SpringContextUtils;
import com.fdb.efp.esb.service.bo.req.yed.YedCompanyInfoRequestBean;
import com.fdb.efp.esb.service.bo.req.yed.YedGjjAccInfoReqBean;
import com.fdb.efp.esb.service.bo.req.yed.YedGjjCompnayInfoRequestBean;
import com.fdb.efp.esb.service.bo.resp.yed.YedCompanyInfoReqBean;
import com.fdb.efp.esb.service.bo.resp.yed.YedGjjCompanyInfoResponseBean;
import com.fdb.efp.esb.service.bo.resp.yed.YedGjjAccNoInfoResponseBean;
import com.fdb.efp.esb.service.client.ESBClientFactory;
import com.fdb.efp.esb.service.client.impl.YEDESBBeanCreator;
import com.fdb.efp.esb.service.facade.yed.YedCompanyInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * Description:优e贷查询外部平台务处理类
 * Date:2020/8/10 10:54
 * Create by :
 */
@Service("yedCompanyInfoService")
public class YedCompanyInfoServiceImpl implements YedCompanyInfoService {

    private final Logger log = LoggerFactory.getLogger(YedCompanyInfoServiceImpl.class);

    private static final String QryTpId = "25123";//33106：主机对主机_企业报告查询（互联网、专网小额客户适用）
    private static final String ChrgSbrprtTpIdGrp="22523";//21301企业基本信息
    private static final String QryRsnId="101";//101贷款审批


    /** ESB调用外数平台，ESB调用互开*/
    public static final String TRADE_NO_20120005="20120005";
    public static final String YED_GJJ_02="02";
    public static final String TRADE_NO_30730002="30730002";
    public static final String YED_GJJ_03="03";
    public static final String TRADE_NO_40130013="40130013";
    public static final String YED_PY_01="01";

    /**
    *description：查询鹏元
    *param：
    *return:
    *date: 2020/8/11 14:47
    *auther: gcb
    */
    @Override
    public YedCompanyInfoReqBean queryCompanyInfoForIOP(YedCompanyInfoRequestBean requestBean) throws Exception {
        if(StringUtils.isBlank(requestBean.getEntpNm())){
            log.info("公司名称为空");
        }
        YedCompanyInfoReqBean yedCompanyInfoReqBean = null;
        requestBean.setQryTpId(QryTpId);
        requestBean.setChrgSbrprtTpIdGrp(ChrgSbrprtTpIdGrp);
        requestBean.setQryRsnId(QryRsnId);
        EsbRespServiceBean esbRespServiceBean = ESBClientFactory.
                buildClient().
                withTradeNo(TRADE_NO_40130013).
                withScene(YED_PY_01).
                withBody(requestBean).
                create().
                sendAndReceive();
        String retCode = "";
        try{
            // 通过系统头判断是否成功系统头
            EsbRespSysHeadBean sysHead =
                    esbRespServiceBean.getSysHead();
            if (Objects.nonNull(sysHead)) {
                // 返回信息数据不为空
                EsbRespRetInfBean esbRespRetInfBean = sysHead.getRetInfArry().stream().findFirst().orElseGet(EsbRespRetInfBean::new);
                retCode = Optional.ofNullable(esbRespRetInfBean.getRetCd()).orElse(StringUtils.EMPTY);
                if (ESBConstant.SUCCESS.equals(retCode)) {
                    String msg = "调用鹏元查询企业信息成功";
                    log.debug(msg);
                    yedCompanyInfoReqBean = esbRespServiceBean.getBody(YedCompanyInfoReqBean.class);
                } else {
                    String msg = "调用鹏元查询企业信息失败 " + esbRespRetInfBean.getRetMsg();
                    log.error(msg);
                    throw new ESBException(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),msg);
                }
            }
        }catch (Exception e){
            String msg = "调用鹏元查询企业信息异常";
            log.error(msg);
            throw new ESBException(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),msg);
        }
        return yedCompanyInfoReqBean;
    }

    /**
    *description：查询公积金账号
    *param：
    *return:
    *date: 2020/8/11 14:47
    *auther: gcb
    */
    @Override
    public YedGjjCompanyInfoResponseBean queryGJJCompanyInfoForEDS(YedGjjCompnayInfoRequestBean requestBean) throws Exception {
        if(StringUtils.isBlank(requestBean.getIdentNo()) || StringUtils.isBlank(requestBean.getIdentTp()) || StringUtils.isBlank(requestBean.getBeQryPrsnNm())){
            log.info("证件类型，或者证件号码，或者姓名为空!证件类型为：{}，证件号码为：{}，姓名为：{}",requestBean.getIdentTp(),
                    requestBean.getIdentNo(),requestBean.getBeQryPrsnNm());
        }
        YedGjjCompanyInfoResponseBean responseBean = null;
        requestBean.setBnkNo("21");
        requestBean.setCnterNo("授信");
        requestBean.setDeptNo("4001000");
        requestBean.setIntfTxnCd("DS10");
        requestBean.setPlatFlowNo("");
        requestBean.setSvcNo("优e贷");
        requestBean.setSysNo("4001000");
        requestBean.setTxnCd("DS10");
        requestBean.setTxnSrlNo("DS100120171011000001");

        YEDESBBeanCreator yedesbBeanCreator = (YEDESBBeanCreator) SpringContextUtils.getBean("YEDESBBeanCreator");
        yedesbBeanCreator.setMsgTp("Request");
        EsbRespServiceBean esbRespServiceBean = ESBClientFactory.
                buildClient().
                withTradeNo(TRADE_NO_20120005).
                withScene(YED_GJJ_02).
                withBody(requestBean).
                withESBBeanFactory(yedesbBeanCreator).
                create().
                sendAndReceive();
        String retCode = "";
        try{
            // 通过系统头判断是否成功系统头
            EsbRespSysHeadBean sysHead =
                    esbRespServiceBean.getSysHead();
            if (Objects.nonNull(sysHead)) {
                // 返回信息数据不为空
                EsbRespRetInfBean esbRespRetInfBean = sysHead.getRetInfArry().stream().findFirst().orElseGet(EsbRespRetInfBean::new);
                retCode = Optional.ofNullable(esbRespRetInfBean.getRetCd()).orElse(StringUtils.EMPTY);
                if (ESBConstant.SUCCESS.equals(retCode)) {
                    String msg = "调用外数平台查询公积金账号成功";
                    log.debug(msg);
                    responseBean = esbRespServiceBean.getBody(YedGjjCompanyInfoResponseBean.class);
                } else {
                    String msg = "调用外数平台查询公积金账号失败 " + esbRespRetInfBean.getRetMsg();
                    log.error(msg);
                    throw new ESBException(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),msg);
                }
            }
        }catch (Exception e){
            String msg = "调用外数平台查询公积金账号异常";
            log.error(msg);
            throw new ESBException(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),msg);
        }
        return responseBean;
    }

    /**
    *description：查询公积金详细信息
    *param：
    *return:
    *date: 2020/8/11 16:26
    *auther: gcb
    */
    @Override
    public YedGjjAccNoInfoResponseBean queryGJJAccNOInfoForEDS(YedGjjAccInfoReqBean requestBean) throws Exception {
        if(StringUtils.isBlank(requestBean.getPrvdntAcctNo())){
            log.info("公积金账号为空！公积金账号为：{}",requestBean.getPrvdntAcctNo());
        }
        YedGjjAccNoInfoResponseBean responseBean = null;
        requestBean.setBnkNo("21");
        requestBean.setCnterNo("授信");
        requestBean.setDeptNo("4001000");
        requestBean.setIntfTxnCd("DS21");
        requestBean.setPlatFlowNo("");
        requestBean.setSvcNo("优e贷");
        requestBean.setSysNo("4001000");
        requestBean.setTxnCd("DS21");
        requestBean.setTxnSrlNo("DS210120171011000001");
        YEDESBBeanCreator yedesbBeanCreator = (YEDESBBeanCreator) SpringContextUtils.getBean("YEDESBBeanCreator");
        yedesbBeanCreator.setMsgTp("Request");

        EsbRespServiceBean esbRespServiceBean = ESBClientFactory.
                buildClient().
                withTradeNo(TRADE_NO_30730002).
                withScene(YED_GJJ_03).
                withBody(requestBean).
                withESBBeanFactory(yedesbBeanCreator).
                create().
                sendAndReceive();
        String retCode = "";
        // 通过系统头判断是否成功系统头
        try{
            EsbRespSysHeadBean sysHead =
                    esbRespServiceBean.getSysHead();
            if (Objects.nonNull(sysHead)) {
                // 返回信息数据不为空
                EsbRespRetInfBean esbRespRetInfBean = sysHead.getRetInfArry().stream().findFirst().orElseGet(EsbRespRetInfBean::new);
                retCode = Optional.ofNullable(esbRespRetInfBean.getRetCd()).orElse(StringUtils.EMPTY);
                if (ESBConstant.SUCCESS.equals(retCode)) {
                    String msg = "调用外数平台查询公积金账号成功";
                    log.debug(msg);
                    responseBean = esbRespServiceBean.getBody(YedGjjAccNoInfoResponseBean.class);
                } else {
                    String msg = "调用外数平台查询公积金账号失败 " + esbRespRetInfBean.getRetMsg();
                    log.error(msg);
                    throw new ESBException(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),msg);
                }
            }
        }catch (Exception e){
            String msg = "调用外数平台查询公积金账号异常 ";
            log.error(msg);
            throw new ESBException(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),msg);
        }

        return responseBean;
    }
}
